package com.furnitur.modules.portal.service.impl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.furnitur.core.entity.order.Order;
import com.furnitur.core.entity.payment.PaymentRecord;
import com.furnitur.core.mapper.order.OrderMapper;
import com.furnitur.core.mapper.payment.PaymentRecordMapper;
import com.furnitur.modules.portal.dto.payment.PaymentDTO;
import com.furnitur.modules.portal.service.PortalPaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class PortalPaymentServiceImpl implements PortalPaymentService {
    
    @Autowired
    private OrderMapper orderMapper;
    
    @Autowired
    private PaymentRecordMapper paymentRecordMapper;
    
    @Override
    public Map<String, Object> createPayment(PaymentDTO paymentDTO) {
        // 1. 查询订单
        Order order = orderMapper.selectOne(
            new QueryWrapper<Order>()
                .eq("order_no", paymentDTO.getOrderNo())
        );
        
        if (order == null) {
            throw new RuntimeException("订单不存在");
        }
        
        if (order.getStatus() != 0) {
            throw new RuntimeException("订单状态不正确");
        }
        
        // 2. 创建支付记录
        PaymentRecord paymentRecord = new PaymentRecord();
        paymentRecord.setOrderId(order.getId());
        paymentRecord.setPaymentNo(UUID.randomUUID().toString().replace("-", ""));
        paymentRecord.setPaymentMethod(paymentDTO.getPaymentMethod());
        paymentRecord.setAmount(order.getTotalAmount());
        paymentRecord.setStatus(0);
        
        paymentRecordMapper.insert(paymentRecord);
        
        // 3. 返回支付信息（模拟支付二维码等信息）
        Map<String, Object> result = new HashMap<>();
        result.put("paymentNo", paymentRecord.getPaymentNo());
        result.put("amount", paymentRecord.getAmount());
        result.put("qrCode", "模拟支付二维码: " + paymentRecord.getPaymentNo());
        
        return result;
    }
    
    @Override
    public Integer queryPaymentStatus(String orderNo) {
        PaymentRecord paymentRecord = paymentRecordMapper.selectOne(
            new QueryWrapper<PaymentRecord>()
                .eq("order_no", orderNo)
                .orderByDesc("created_at")
                .last("LIMIT 1")
        );
        
        return paymentRecord != null ? paymentRecord.getStatus() : null;
    }
    
    @Override
    @Transactional
    public void mockPaymentCallback(String orderNo) {
        // 1. 查询订单
        Order order = orderMapper.selectOne(
            new QueryWrapper<Order>()
                .eq("order_no", orderNo)
        );
        
        if (order == null) {
            throw new RuntimeException("订单不存在");
        }
        
        // 2. 更新支付记录状态
        PaymentRecord paymentRecord = paymentRecordMapper.selectOne(
            new QueryWrapper<PaymentRecord>()
                .eq("order_id", order.getId())
                .orderByDesc("created_at")
                .last("LIMIT 1")
        );
        
        if (paymentRecord != null) {
            paymentRecord.setStatus(1); // 设置为支付成功
            paymentRecordMapper.updateById(paymentRecord);
            
            // 3. 更新订单状态
            order.setStatus(1); // 设置为待发货
            order.setPaymentTime(new Date());
            orderMapper.updateById(order);
        }
    }
} 